package main

import (
	"context"
	"transfer-service/internal/biz"
	"transfer-service/internal/conf"
	"transfer-service/internal/global"
	"transfer-service/internal/initialize"
	"transfer-service/internal/service"

	"github.com/go-kratos/kratos/v2"
	"github.com/go-kratos/kratos/v2/transport/http"
	"github.com/google/uuid"
	"github.com/jinzhu/copier"
	zlog "github.com/jiushengTech/common/log/zap"
	cconf "github.com/jiushengTech/common/log/zap/conf"
)

// go build -ldflags "-X main.Version=x.y.z"

func newApp(
	c *conf.Bootstrap,
	antiCarTcpServer *biz.AntiCarTcpServer,
	antiGunTcpServer *biz.AntiGunTcpServer,
	antiService *service.Anti,
	hs *http.Server,
) *kratos.App {
	return kratos.New(
		kratos.ID(uuid.New().String()),
		kratos.Name(c.Profiles.Name),
		kratos.Version(c.Profiles.Version),
		kratos.Metadata(map[string]string{}),
		kratos.Server(
			antiCarTcpServer,
			antiGunTcpServer,
			hs,
		),
		kratos.AfterStart(func(context.Context) error {
			antiService.StartTransfer()
			return nil // 假设 StartTransfer 不返回错误
		}),
	)
}

func main() {
	initialize.InitAll()
	c := cconf.ZapConf{}
	zapConf := global.BC.GetInfra().GetLog().GetZap()
	if err := copier.Copy(&c, zapConf); err != nil {
		panic(err)
	}
	c.AddCallerSkip = 3
	logger := zlog.NewZapLogger(&c)
	app, cleanup, err := wireApp(
		global.BC,
		&initialize.Logger{Logger: logger},
	)
	if err != nil {
		panic(err)
	}
	defer cleanup()
	// start and wait for stop signal
	if err := app.Run(); err != nil {
		panic(err)
	}
}
